import 'dart:async';

import 'package:get/get.dart';
import 'package:xinxuan/controllers/store_order_controller/store_order_page_controller.dart';
import 'package:xinxuan/http/http_util_service.dart';
import 'package:xinxuan/vo/order_detail_vo.dart';
import 'package:common_utils/common_utils.dart';

class StoreOrderPaySuccessDetailPageController extends GetxController {

  final StoreOrderPageController storeOrderPageController = Get.put(StoreOrderPageController());

  /// 订单详情数据
  var orderDetailData = OrderDetailData(id: 0, status: 1, isCheck: 0, pickType: 2, transferRemaining: 0, totalAmount: '0.00').obs;

  /// 倒计时剩余时间
  var duration = 0.obs;
  var transferRemainingTimeStr = '00:00:00'.obs;

  /// 时间倒计时
  Timer? _timer;
  List<String> tipArr = [];
  int orderId = 0;
  
  @override
  void onReady() {
    Map<String, dynamic> argumentsMap = Get.arguments;
    orderId = argumentsMap['orderId'];
    getStoreOrderDetail(orderId);
    super.onReady();
  }

  /// 获取订单详情
  getStoreOrderDetail(int orderId) async {
    OrderDetailVo orderDetailVo = await HttpUtilService.getStoreOrderDetail(orderId: orderId);
    orderDetailData.value = orderDetailVo.data!;
    if (!TextUtil.isEmpty(orderDetailData.value.tips)) {
      orderDetailData.value.tips = orderDetailData.value.tips!.replaceAll('\\n', '\n');
      tipArr = orderDetailData.value.tips!.split('{transfer_remaining}');
    }
    if (orderDetailData.value.status == 1) {
      _calculationTime();
    } else {
      _calculationTransferRemainingTime();
    }
    // _calculationTransferRemainingTime();
    update();
  }

  /// 计算时间
  _calculationTime() {
    duration.value = orderDetailData.value.remainingTime ?? 0;
    if (duration.value <= 0) {
      return;
    }
    print('duration.value: ${duration.value}');
    getTransferRemainingTimeStr();     
    _timer?.cancel();
    _timer = Timer.periodic(Duration(seconds: 1), (timer) {
      duration.value--;
      print('duration.value: ${duration.value }');
      getTransferRemainingTimeStr();      
      if (duration.value == 0) {
        _timer?.cancel();
        getStoreOrderDetail(orderId);
        return;
      }
    });
  }
  
  //外面传进来的单位是秒，所以需要根据总秒数，计算小时，分钟，秒
  int hour = 0;
  int minute = 0;
  int second = 0;

  /// 计算可转让时间时间
  _calculationTransferRemainingTime() {
    if (orderDetailData.value.pickType != 1 || orderDetailData.value.transferRemaining! <= 0) {
      return;
    }
    duration.value = orderDetailData.value.transferRemaining ?? 0;
    getTransferRemainingTimeStr();
    _timer?.cancel();
    _timer = Timer.periodic(Duration(seconds: 1), (timer) {
      duration.value--;
      print('_calculationTransferRemainingTime: ${duration.value }');
      getTransferRemainingTimeStr();      
      if (duration.value == 0) {
        _timer?.cancel();
        getStoreOrderDetail(orderId);
        return;
      }
    });
  }

  getTransferRemainingTimeStr() {
    //外面传进来的单位是秒，所以需要根据总秒数，计算小时，分钟，秒
    hour = (duration.value ~/ 3600);
    minute = duration.value % 3600 ~/60;
    second = duration.value % 60;

    var str = '';
    if (hour / 10 < 1) {
      str = str + '0' + hour.toString() + ":";
    } else {
      str = str + hour.toString()+':';
    }

    if (minute / 10 < 1) {//当只有个位数时，给前面加“0”，实现效果：“:01”,":02"
      str = str + '0' + minute.toString() + ":";
    } else {
      str = str + minute.toString() + ":";
    }

    if (second / 10 < 1) {
      str = str + '0' + second.toString();
    } else {
      str = str + second.toString();
    }
    transferRemainingTimeStr.value = str;
    print('str: $str');
  }

  @override
  void onClose() {
    _timer?.cancel();
    super.onClose();
  }
}